package com.alix.paymoney.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.alix.paymoney.R;
import com.alix.paymoney.base.BaseActivity;
import com.alix.paymoney.fragment.AllFragment;
import com.alix.paymoney.fragment.HomeFragment;
import com.alix.paymoney.fragment.MeFragment;
import com.alix.paymoney.utils.ToasUtil;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.fragment_container)
    FrameLayout fragmentContainer;
    @BindView(R.id.tab_home)
    RadioButton tabHome;
    @BindView(R.id.tab_all)
    RadioButton tabAll;
    @BindView(R.id.tab_me)
    RadioButton tabMe;
    @BindView(R.id.tab_bar)
    LinearLayout tabBar;

    HomeFragment homeFragment;
    AllFragment allFragment;
    MeFragment meFragment;
    private List<Fragment> fragmentList;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void afterCreate(@Nullable Bundle savedInstanceState) {
        fragmentList=new ArrayList<>();
        homeFragment=new HomeFragment();
        allFragment=new AllFragment();
        meFragment=new MeFragment();
        fragmentList.add(homeFragment);
        fragmentList.add(allFragment);
        fragmentList.add(meFragment);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, homeFragment, "home")
                .add(R.id.fragment_container, allFragment, "all")
                .show(homeFragment)
                .hide(allFragment)
                .commit();

    }


    private long lastTime;
    private void showFragment(int i, String tag) {
        for (int j = 0; j < fragmentList.size(); j++) {
            Fragment fragment = fragmentList.get(j);
            if (i == j) {
                if (fragment.isAdded()) {
                    getSupportFragmentManager().beginTransaction().show(fragment).commit();
                } else {
                    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fragment, tag).commit();
                }


            } else {
                if (fragment.isAdded()) {
                    getSupportFragmentManager().beginTransaction().hide(fragment).commit();
                }

            }
        }
    }

    @Override
    public void onBackPressed() {

        if (System.currentTimeMillis() - lastTime > 1000) {
            lastTime = System.currentTimeMillis();
            ToasUtil.showCenterToast("再按一次退出");
        } else {

            super.onBackPressed();
        }

    }

   @OnCheckedChanged({R.id.tab_home, R.id.tab_all, R.id.tab_me})
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       switch (buttonView.getId()) {
           case R.id.tab_home:
               showFragment(0,"home");
               break;
           case R.id.tab_all:
               showFragment(1,"all");
               break;
           case R.id.tab_me:
               showFragment(2,"me");
               break;
       }
    }


}
